//
//  YWReplyViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/11/5.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWReplyViewController.h"
#import <UIImageView+WebCache.h>
#import "MLPhotoBrowserViewController.h"

@interface YWReplyViewController () <MLPhotoBrowserViewControllerDataSource,MLPhotoBrowserViewControllerDelegate>

@property (nonatomic,strong) NSArray *contentArray;

@property (nonatomic,strong) UIView *headerView;

@end

@implementation YWReplyViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (NSArray *)contentArray{
    if (_contentArray == nil) {
        _contentArray = @[@"编号",@"货号",@"价格",@"备注",@"档口名称",@"档口地址",@"档口电话",@"回复时间"];
    }
    
    return _contentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"回复详情";
    
    // 设置底部按钮
    [self setupButtomMenu];
}



- (void)setupButtomMenu{
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    [contentView setCenter:CGPointMake(BBScreenW - 50, BBScreenH - 120)];
    [contentView setBackgroundColor:BBRGBColor(85, 26, 175)];
    [contentView.layer setCornerRadius:30.];
    
    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cross"]];
    [icon setContentMode:UIViewContentModeScaleAspectFit];
    icon.frame = CGRectMake(0, 0, 20, 20);
    icon.center = CGPointMake(contentView.width * 0.5, contentView.width * 0.5);
    [contentView addSubview:icon];
    
    [self.view addSubview:contentView];
    
    [contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttomClick)]];
    
}

- (void)buttomClick{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"联系电话" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"联系档口" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIApplication *app = [UIApplication sharedApplication];
        NSString *string = [NSString stringWithFormat:@"telprompt://%@",self.reply.sellerTel];
        [app openURL:[NSURL URLWithString:string]];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"联系客服" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIApplication *app = [UIApplication sharedApplication];
        [app openURL:[NSURL URLWithString:@"telprompt://4000680040"]];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 100;
}

// headerview
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BBScreenW, 100)];
    self.headerView = headerView;
    
    CGFloat imageW = 80;
    CGFloat imageH = 80;
    CGFloat imageY = 10;
    CGFloat space = 10;     // 间距
    
    for (int i = 0; i < self.reply.urls.count; i++) {
        CGFloat imageX = i * (imageW + space) + space;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        
        imageView.tag = i + 1;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick:)]];
        
        NSURL *url = [NSURL URLWithString:self.reply.urls[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_no_img"]];
        [self.headerView addSubview:imageView];
    }
    
    return headerView;
}

- (void)imageViewClick:(UITapGestureRecognizer *)gesture{
    //    NSLog(@"%@",gesture.view);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag - 1 inSection:0];
    // 图片游览器
    MLPhotoBrowserViewController *photoBrowser = [[MLPhotoBrowserViewController alloc] init];
    // 淡入淡出效果
    photoBrowser.status = UIViewAnimationAnimationStatusFade;
    // 数据源/delegate
    photoBrowser.delegate = self;
    photoBrowser.dataSource = self;
    // 当前选中的值
    photoBrowser.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
    // 展示控制器
    [photoBrowser show];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 5) {
        return 88;
    }
    
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    
    // 取消选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.textLabel.text = self.contentArray[indexPath.row];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 31)];
    label.numberOfLines = 3;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentRight;
    label.tag = indexPath.row;
    // 设置文字
    [self setText:label];
    
    // 档口地址
    if (indexPath.row == 5) {
        label.textAlignment = NSTextAlignmentLeft;
        label.frame = CGRectMake(0, 0, 250, 62);
    }
    
    cell.accessoryView = label;
    
    return cell;
    
}

- (void)setText:(UILabel *)label{
    switch (label.tag) {
        case 0: // 编号
            label.text = self.reply.ID;
            break;
        case 1: // 货号
            label.text = self.reply.productName;
            break;
        case 2: // 价格
            label.text = self.reply.productPrice;
            break;
        case 3: // 备注
            label.text = self.reply.replyDescription;
            break;
        case 4: // 档口名称
            label.text = self.reply.sellerName;
            break;
        case 5: // 档口地址
            label.text = self.reply.sellerAddress;
            break;
        case 6: // 档口电话
            label.text = self.reply.sellerTel;
            break;
        case 7: // 回复时间
            label.text = [self getTime:self.reply.createTime];
            break;
        default:
            break;
    }
}

// 计算时间
- (NSString *)getTime:(NSString *)time{
    
    NSTimeInterval timeInterval = [time doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd HH:mm";
    
    return [formatter stringFromDate:date];
    
}

#pragma mark - <MLPhotoBrowserViewControllerDataSource>
- (NSInteger)photoBrowser:(MLPhotoBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{

    return self.reply.urls.count;
}

#pragma mark - 每个组展示什么图片,需要包装下MLPhotoBrowserPhoto
- (MLPhotoBrowserPhoto *) photoBrowser:(MLPhotoBrowserViewController *)browser photoAtIndexPath:(NSIndexPath *)indexPath{
    // 包装下imageObj 成 ZLPhotoPickerBrowserPhoto 传给数据源
    MLPhotoBrowserPhoto *photo = [[MLPhotoBrowserPhoto alloc] init];
    photo.photoObj = [self.reply.urls objectAtIndex:indexPath.row];
    // 缩略图
    //    UIButton *btn = self.scrollView.subviews[indexPath.row];
    //    photo.thumbImage = btn.imageView.image;
    return photo;
}


@end